# Maintainer: Ricky Wu <rickleaf.wu@gmail.com>

_realname=lgi
pkgbase=mingw-w64-lua-${_realname}
pkgname=(
  "${MINGW_PACKAGE_PREFIX}-lua-${_realname}"
  "${MINGW_PACKAGE_PREFIX}-lua51-${_realname}"
  "${MINGW_PACKAGE_PREFIX}-lua53-${_realname}"
)
pkgver=0.9.2
pkgrel=6
pkgdesc="LGI is gobject-introspection based dynamic Lua binding to GObject based libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/pavouk/lgi"
license=("spdx:MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-lua"
             "${MINGW_PACKAGE_PREFIX}-lua51"
             "${MINGW_PACKAGE_PREFIX}-lua53")
depends=("${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gobject-introspection-runtime"
         "${MINGW_PACKAGE_PREFIX}-libffi")
source=("https://github.com/pavouk/${_realname}/archive/${pkgver}/${_realname}-${pkgver}.zip"
        "lgi-lua54.patch::https://github.com/pavouk/lgi/pull/249.patch")
sha256sums=('c6cdb7cca6b745790454b45ebb9c3e8be96aca948c7d640d79c969a1c78b35e9'
            'eaa5a39883bc76c77bea3fbf4ec0a8d60444bf78c6ff8e2096a550d30b986f29')

prepare() {
  patch -d "${_realname}-${pkgver}" -p1 -i "${srcdir}/lgi-lua54.patch"

  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}-lua"

  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}-lua51"
  sed -i "s/-llua/-llua5.1/g" "build-${MSYSTEM}-lua51"/lgi/Makefile

  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}-lua53"
  sed -i "s/-llua/-llua5.3/g" "build-${MSYSTEM}-lua53"/lgi/Makefile
}

build() {
  CFLAGS="$CFLAGS -I${MINGW_PREFIX}/include" make -C "build-${MSYSTEM}-lua" all
  CFLAGS="$CFLAGS -I${MINGW_PREFIX}/include/lua5.1" make -C "build-${MSYSTEM}-lua51" all
  CFLAGS="$CFLAGS -I${MINGW_PREFIX}/include/lua5.3" make -C "build-${MSYSTEM}-lua53" all
}

package_lua-lgi() {
  depends+=("${MINGW_PACKAGE_PREFIX}-lua")

  CFLAGS="$CFLAGS -I${MINGW_PREFIX}/include" \
    make -C "build-${MSYSTEM}-lua" \
    LUA_VERSION=5.4 \
    PREFIX="${MINGW_PREFIX}" DESTDIR="${pkgdir}" install
}

package_lua51-lgi() {
  depends+=("${MINGW_PACKAGE_PREFIX}-lua51")

  CFLAGS="$CFLAGS -I${MINGW_PREFIX}/include/lua5.1" \
    make -C "build-${MSYSTEM}-lua51" \
    LUA_VERSION=5.1 \
    PREFIX="${MINGW_PREFIX}" DESTDIR="${pkgdir}" install
}

package_lua53-lgi() {
  depends+=("${MINGW_PACKAGE_PREFIX}-lua53")

  CFLAGS="$CFLAGS -I${MINGW_PREFIX}/include/lua5.3" \
    make -C "build-${MSYSTEM}-lua53" \
    LUA_VERSION=5.3 \
    PREFIX="${MINGW_PREFIX}" DESTDIR="${pkgdir}" install
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
